package org.ucn.dmaa0914.temadesign.group2.tuiLayer;

import java.util.Date;
import org.ucn.dmaa0914.temadesign.group2.modelLayer.Loan;
import org.ucn.dmaa0914.temadesign.group2.controlLayer.LoanController;

/**
 * Write a description of class LoanMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanMenu extends Menu
{
    private int entryCreate;
    private int entryEdit;
    private int entryAddCopy;
    private int entryDelete;
    private int entrySelect;
    private int entryList;
    private int entryDetails;
    
    private LoanController controller;
    
    public LoanMenu(Menu parentMenu)
    {
        super(parentMenu, "Loan Menu");
        
        entryCreate = addEntry("Create Loan for Selected Person");
        entryEdit = addEntry("Edit Selected Loan");
        entryAddCopy = addEntry("Add Selected Copy to Selected Loan");
        entryDelete = addEntry("Delete Selected Loan");
        entrySelect = addEntry("Select Loan from ID");
        entryList = addEntry("List All Lons for the Selected Person");
        entryDetails = addEntry("Show Details about Selected Loan");
        
        controller = new LoanController();
    }
    
    public void handleEntry(int entryIndex)
    {
        super.handleEntry(entryIndex);
        
        if( entryIndex == entryDetails && Application.instance.getSelectedLoan() != null )
        {
            Application.instance.setResultText(Application.instance.getSelectedLoan().makeDescription());
        }
        
        if( entryIndex == entryDelete && Application.instance.getSelectedLoan() != null )
        {
            Loan p = Application.instance.getSelectedLoan();
            controller.deleteLoan(p);
        }
        
        if( entryIndex == entrySelect )
        {
            String id = prompt("Enter ID: ");
            Application.instance.setSelectedLoan(controller.getLoan(Integer.parseInt(id)));
        }
        
        if( entryIndex == entryAddCopy && Application.instance.getSelectedLoan() != null && Application.instance.getSelectedCopy() != null )
        {
            controller.addCopy(Application.instance.getSelectedLoan(), Application.instance.getSelectedCopy());
        }
        
        if( entryIndex == entryList && Application.instance.getSelectedPerson() != null )
        {
            String result = "";
            for( Loan l : controller.findLoans(Application.instance.getSelectedPerson()) )
            {
                result += l.toString() + "\n";
            }
            
            Application.instance.setResultText(result);
        }
        
        if( entryIndex == entryCreate && Application.instance.getSelectedPerson() != null )
        {
            Loan p = controller.createLoan(Application.instance.getSelectedPerson(), new Date(), new Date());
            
            // Start editing the new Loan
            Application.instance.setSelectedLoan(p);
            handleEntry(entryEdit);
        }
        
        if( entryIndex == entryEdit && Application.instance.getSelectedLoan() != null )
        {
            Loan p = Application.instance.getSelectedLoan();
            
            String fromDate = prompt("Enter Loan Start Date (Empty for no change): ");
            String toDate = prompt("Enter Loan End Date (Empty for no change): ");
            
            if( fromDate.length() == 0 )
            {
                fromDate = Application.dateFormat.format(p.getBorrowDateFrom());
            }
            
            if( toDate.length() == 0 )
            {
                toDate = Application.dateFormat.format(p.getBorrowDateTo());
            }
            
            Date fromDateObj = p.getBorrowDateFrom();
            Date toDateObj = p.getBorrowDateTo();
            
            try
            {
                fromDateObj = Application.dateFormat.parse(fromDate);
                toDateObj = Application.dateFormat.parse(toDate);
            }
            catch(java.text.ParseException e)
            {
            }
            
            controller.updateLoan(p, fromDateObj, toDateObj);
        }
    }
}
